#include "mainwindow.h"
#include <QFileDialog>
#include <QStandardPaths>
#include <QList>
#include <QMediaMetaData>
#include <QApplication>
#include <QTime>



MainWindow::MainWindow(QWidget *parent)
    :QWidget(parent)
{

}

MainWindow::~MainWindow()
{

}
void MainWindow::setModel(PlayListModel* model)
{
    m_model = model;
}

void MainWindow::setPlayer(QMediaPlayer* player)
{
    m_player = player;

    connect(m_player, SIGNAL(metaDataChanged()), SLOT(metaDataChanged()));
    connect(m_player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
            this, SLOT(statusChanged(QMediaPlayer::MediaStatus)));
    connect(m_player, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int)));
    connect(m_player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(displayErrorMessage()));
}

void MainWindow::open()
{
    QFileDialog fileDialog;
    fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
    fileDialog.setWindowTitle(tr("Open Files"));
    QStringList supportedMimeTypes = m_player->supportedMimeTypes();
    if (!supportedMimeTypes.isEmpty()) {
        supportedMimeTypes.append("audio/x-m3u"); // MP3 playlists
        fileDialog.setMimeTypeFilters(supportedMimeTypes);
    }
    fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::MoviesLocation).value(0, QDir::homePath()));
    if (fileDialog.exec() == QDialog::Accepted)
        m_model->addToPlaylist(fileDialog.selectedUrls());
}


void MainWindow::metaDataChanged()
{
    if (m_player->isMetaDataAvailable()) {
        setTrackInfo(QString("%1 - %2")
                .arg(m_player->metaData(QMediaMetaData::AlbumArtist).toString())
                .arg(m_player->metaData(QMediaMetaData::Title).toString()));
    }
}

QString MainWindow::getPlayerUrl() const
{
        QUrl url = m_player->metaData(QMediaMetaData::CoverArtUrlLarge).value<QUrl>();
        return url.isEmpty()?url.toString():"";
}

void MainWindow::statusChanged(QMediaPlayer::MediaStatus status)
{
    handleCursor(status);

    switch (status) {
    case QMediaPlayer::UnknownMediaStatus:
    case QMediaPlayer::NoMedia:
    case QMediaPlayer::LoadedMedia:
    case QMediaPlayer::BufferingMedia:
    case QMediaPlayer::BufferedMedia:
        setStatusInfo(QString());
        break;
    case QMediaPlayer::LoadingMedia:
        setStatusInfo(tr("Loading..."));
        break;
    case QMediaPlayer::StalledMedia:
        setStatusInfo(tr("Media Stalled"));
        break;
    case QMediaPlayer::EndOfMedia:
        QApplication::alert(this);
        break;
    case QMediaPlayer::InvalidMedia:
        displayErrorMessage();
        break;
    }
}

void MainWindow::handleCursor(QMediaPlayer::MediaStatus status)
{
#ifndef QT_NO_CURSOR
    if (status == QMediaPlayer::LoadingMedia ||
        status == QMediaPlayer::BufferingMedia ||
        status == QMediaPlayer::StalledMedia)
        setCursor(QCursor(Qt::BusyCursor));
    else
        unsetCursor();
#endif
}

void MainWindow::bufferingProgress(int progress)
{
    setStatusInfo(tr("Buffering %4%").arg(progress));
}


void MainWindow::setTrackInfo(const QString &info)
{
    trackInfo = info;
    if (!statusInfo.isEmpty())
        emit messageChanged(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
    else
        emit messageChanged(trackInfo);
}

void MainWindow::setStatusInfo(const QString &info)
{
    statusInfo = info;
    if (!trackInfo.isEmpty())
        emit messageChanged(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
    else
        emit messageChanged(statusInfo);

}

void MainWindow::displayErrorMessage()
{
    setStatusInfo(m_player->errorString());
}

